package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    @RequestMapping("/getUsername")
    public Result getUsername(){
        //当Spring security完成认证后,会将当前用户信息保存到框架提供的上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    //返回权限,存储到localStorage中
    @RequestMapping("/getUsername2")
    public Result getUsername2(){
        Map map = new HashMap();
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            map.put("username", user.getUsername());//当前登录用户名
            Set<String> auths = AuthorityUtils.authorityListToSet(user.getAuthorities());
            map.put("auths", auths);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, map);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    //分页查询--
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userService.pageQuery(queryPageBean);
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds) {
        try {
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String encode = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(encode);
            userService.add(user, roleIds);
            System.out.println(encode);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_USER_SUCCESS);
    }


    //删除用户
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            userService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    }
    //根据用户id查询用户关联的权限id
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id) {
        try {
            List<Integer> roleIds = userService.findRoleIdsByUserId(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //数据回显
    @RequestMapping("/findById")
    public Result findById(Integer id) {

        try {
            com.itheima.pojo.User user = userService.findById(id);
            user.setPassword("");
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    //编辑用户
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.itheima.pojo.User user, Integer[] roleIds) {
        try {
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String encode = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(encode);
            userService.edit(user, roleIds);

        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }
}